package utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Objects;
import java.util.Properties;

public class DButils {
    private static String driver;
    private static String url;
    private static String user;
    private static String password;
    private static Connection connection;

    static {
        try {
        Properties properties = new Properties();
        InputStream inputStream = DButils.class.getResourceAsStream( "/jdbc.properties" );
        properties.load( inputStream );
        driver = properties.getProperty( "driver" );
        url = properties.getProperty( "url" );
        user = properties.getProperty( "user" );
        password = properties.getProperty( "password" );
       connection=DriverManager.getConnection( url,user,password );
        Class.forName(driver);
        properties.load( inputStream );
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
        public static ResultSet query (String sql, Objects...param) throws SQLException {
            PreparedStatement ps = connection.prepareStatement( sql );
            for (int i=0;i<param.length;i++){
                ps.setObject( i+1,param[i] );
            }
        return ps.executeQuery();
    }
    public  static int update (String sql,Objects...param) throws SQLException {
        PreparedStatement ps=connection.prepareStatement(  sql);
        for (int i=0;i<param.length;i++){
            ps.setObject( i+1,param[i] );
        }
   return ps.executeUpdate(); }
}